Page({
  data: {
    schema:[
    ],
    formData:{
    },
    title: '',
    result: '',
    remark: '',
    apiName: '',
    inputValue: '',
    range: {
      start: 0,
      end: 0
    },
    selectedText: '',
    showResult: false,
    inputFocus: false,
  },
  onInput(e) {
    this.setData({
      inputValue: e.detail.value
    });
  },
// 监听输入框获取焦点
onFocus() {
  this.setData({
    inputFocus: true,
    errorMsg: ''
  });
},

// 监听输入框失去焦点
onBlur() {
  this.setData({
    inputFocus: false
  });
},
  // 监听选中文本变化
  onSelectionChange(e) {
    this.setData({
      range: {
        start: e.detail.start,
        end: e.detail.end
      }
    });
  },
  // 获取选中的文本范围
  getSelectedRange(e) {
    const that = this;
    console.log(that.data.inputFocus)
    // 检查是否有焦点，如果没有则先获取焦点
    if (!that.data.inputFocus) {
      that.setData({ inputFocus: true });
      
      // 延迟一下，确保焦点已经获取
      setTimeout(() => {
        that.getSelectedTextRange(e);
      }, 300);
    } else {
      that.getSelectedTextRange(e);
    }
  },
  getSelectedTextRange(e) {
    let that = this
    console.log("进来了")
    wx.getSelectedTextRange({
      success(res) {
        console.log('获取选中文本范围成功:', res);     
        // 更新选中范围数据
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname,
          range: {
            start: res.start,
            end: res.end
          },
          // // 计算选中的文本内容
          // selectedText: that.data.inputValue.substring(res.start, res.end),
          showResult: true
        });
      },
      fail(err) {
        console.error('获取选中文本范围失败:', err);
        wx.showToast({
          title: '获取失败',
          icon: 'none'
        });
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      },complete: res => {
        console.log('hideKeyboard res', res)
      }
    })
  },
  hideKeyboard(e) {
    let that = this
    wx.hideKeyboard({
      success: (res) => {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  onKeyboardHeightChange(e){
    // 保存当前实例的引用
    const that = this;
    // 创建键盘高度变化监听函数
    this.keyboardListener = function(res) {
      console.log('键盘高度变化:', res);
      that.setData({
        result: res,
        apiName: e.currentTarget.dataset.apiname 
      })
    }
     // 开始监听
     wx.onKeyboardHeightChange(this.keyboardListener);
  },
  offKeyboardHeightChange(e){
    const that = this;
    if (this.keyboardListener) {
      // 移除监听
      wx.offKeyboardHeightChange(this.keyboardListener);
      this.keyboardListener = null;
      that.setData({
        result: "已移除键盘高度变化事件的监听函数",
        apiName: e.currentTarget.dataset.apiname 
      })
    }else{
      that.setData({
        result: "移除键盘高度变化事件的监听函数失败",
        apiName: e.currentTarget.dataset.apiname 
      })
    }
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  }
})
